package me.xiaoyan.ehr.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Repository;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Repository
public class VerifyTokenDao {
    @Value("${spring.mail.from}")
    private String sender;

    private JavaMailSender javaMailSender;

    @Autowired
    public void setJavaMailSender(JavaMailSender javaMailSender) {
        this.javaMailSender = javaMailSender;
    }

    public void sendSimple(String email, String text, String subject) {
        // 简单邮件消息
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject(subject);
        message.setText(text);
        message.setTo(email);
        message.setFrom(sender);
        javaMailSender.send(message);
    }

    public void sendHTML(String email, String html, String subject) throws MessagingException {
        // 创建一个有内容类型的消息
        MimeMessage message = javaMailSender.createMimeMessage();
        // 消息内容设置助手
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        // 消息内容
        helper.setText(html, true); // 设置为html类型
        // 消息主题
        helper.setSubject(subject);
        // 接收者
        helper.setTo(email);
        // 发送者
        helper.setFrom(sender);
        // 发送
        javaMailSender.send(message);
    }
}
